const Controller = require("egg").Controller;

class StudendController extends Controller {
    // 添加数据
    async create() {
        let name = this.ctx.request.body.name;
        let achievement = this.ctx.request.body.achievement;
        let clazz_id = this.ctx.request.body.clazz_id;
        await this.app.model.Student.create({
            name: name,
            achievement: achievement,
            clazz_id: clazz_id
        })
        console.log(this.ctx.request.body.name)
        console.log(this.ctx.request.body.name)
        console.log(this.ctx.request.body.name)
        console.log(this.ctx.request.body.name)
        console.log(this.ctx.request.body.name)
        this.ctx.body = "添加成功"
    }
    // 查看数据
    async index() {
        let studentList = await this.app.model.Student.findAll({
            include: {
                model: this.ctx.model.Clazz,
                as:"clazz"
            }
        });
        this.ctx.body = studentList;
    }
    // 修改数据
    async update() {
        let id = this.ctx.params.id;
        let name = this.ctx.request.body.name;
        let achievement = this.ctx.request.body.achievement;
        let clazz_id = this.ctx.request.body.clazz_id;
        await this.app.model.Student.update({
            name: name,
            achievement: achievement,
            clazz_id: clazz_id
        }, {
            where: {
                id
            }
        })
        this.ctx.body = "修改成功"
    }
    // 删除数据
    async destroy() {
        let id = this.ctx.params.id;
        await this.app.model.Student.destroy({
            where: {
                id
            }
        })
        console.log(id)
        this.ctx.body = "删除成功！"
    }
}

module.exports = StudendController;